import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import duty from './modules/duty';
import insuranceKnowledge from "./modules/insuranceKnowledge";
import userCenter from './modules/userCenter';
import account from './modules/account';
import { useStore as useMenuStore } from "@/store/menu";
import { $freeAdminNodeList } from '@/api/free/free';
import { useStore as useAuthStore } from "@/store/auth";
import checkAuth from '@/common/checkAuth';
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'index',
    component: () => import("@/views/index"),
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import("@/views/home/home"),
      },
      {
        path: '/Supermarketlist',
        name: 'Supermarketlist',
        component: () => import("@/views/Supermarketlist/Supermarketlist"),
      },
      {
        path: '/GroupList',
        name: 'GroupList',
        component: () => import("@/views/GroupList/GroupList"),
      },
      {
        path: '/GroupDetail',
        name: 'GroupDetail',
        component: () => import("@/views/GroupDetail/GroupDetail"),
      },
      {
        path: '/PersInsureList',
        name: 'PersInsureList',
        component: () => import("@/views/PersInsureList/PersInsureList"),
      },
      duty,
      {
        path: '/safetyDetail',
        name: 'safetyDetail',
        component: () => import("@/views/safetyDetail/safetyDetail"),
      },
      {
        path: '/dataService',
        name: 'dataService',
        component: () => import("@/views/dataService/dataService"),
      },
      insuranceKnowledge,
      {
        path: '/PersInsureDetail',
        name: 'PersInsureDetail',
        component: () => import("@/views/PersInsureDetail/PersInsureDetail"),
      },
      {
        path: '/propertyLiability/details',
        name: 'propertyLiabilityDetails',
        component: () => import("@/views/propertyLiability/details/details"),
      },
      {
        path: '/AccidentInsurePage',
        name: 'AccidentInsurePage',
        component: () => import("@/views/AccidentInsurePage/AccidentInsurePage"),
      },
    ]
  },
  userCenter,
  account,
  {
    path: '/transactionRecords',
    name: 'transactionRecords',
    component: () => import("@/views/transactionRecords/transactionRecords"),
  },
  {
    path: '/register',
    name: 'register',
    component: () => import("@/views/register/register"),
  },
  {
    path: '/forget',
    name: 'forget',
    component: () => import("@/views/forget/forget"),
  },
  {
    path: '/403',
    name: '403',
    component: () => import("@/views/403/403"),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

router.beforeEach((to: any, from: any, next: any) => {
  if(to.meta.auth && !checkAuth(to.meta.auth)) {
    next('/403');
  } else {
    next();
  }
})


//获取权限列表
const getAuthList = () => {
  const authStore = useAuthStore();
  return new Promise((resolve) => {
    $freeAdminNodeList().then((res: any) => {
      authStore.setAuths(res?.data?.perms || []);
      resolve(true);
    }).catch(() => {
      resolve(false);
    })
  })
};

export default router
